package com.lh1010.s3.test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.*;

import java.io.File;
import java.util.Iterator;

public class DynamicJsonFromSchema {

    private static final ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws Exception {
        // 读取你的 Amazon JSON Schema 文件
        File schemaFile = new File("C:\\Users\\Admin\\Desktop\\eu-SHELF.xml");
        JsonNode schema = mapper.readTree(schemaFile);

        // 构建输出的 JSON 示例
        ObjectNode example = generateExampleFromSchema(schema);
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(example));
    }

    public static ObjectNode generateExampleFromSchema(JsonNode schema) {
        ObjectNode result = mapper.createObjectNode();

        if (schema.has("properties")) {
            Iterator<String> fieldNames = schema.get("properties").fieldNames();
            while (fieldNames.hasNext()) {
                String fieldName = fieldNames.next();
                JsonNode fieldSchema = schema.get("properties").get(fieldName);

                if (fieldSchema.isObject()) {
                    result.set(fieldName, generateFieldExample(fieldSchema));
                }
            }
        }

        return result;
    }

    private static JsonNode generateFieldExample(JsonNode fieldSchema) {
        if (fieldSchema.has("type")) {
            String type = fieldSchema.get("type").asText();

            switch (type) {
                case "string":
                    return handleStringType(fieldSchema);

                case "integer":
                    return mapper.valueToTree(123);

                case "number":
                    return mapper.valueToTree(123.45);

                case "boolean":
                    return mapper.valueToTree(true);

                case "array":
                    return handleArrayType(fieldSchema);

                case "object":
                    return generateExampleFromSchema(fieldSchema);
            }
        }

        if (fieldSchema.has("enum")) {
            // 如果有枚举值，选第一个
            return fieldSchema.get("enum").get(0);
        }

        return mapper.nullNode();
    }

    private static JsonNode handleStringType(JsonNode fieldSchema) {
        if (fieldSchema.has("examples")) {
            return fieldSchema.get("examples").get(0);
        } else if (fieldSchema.has("enum")) {
            return fieldSchema.get("enum").get(0);
        } else {
            return mapper.valueToTree("example_value");
        }
    }

    private static ArrayNode handleArrayType(JsonNode fieldSchema) {
        ArrayNode array = mapper.createArrayNode();

        if (fieldSchema.has("items") && fieldSchema.get("items").isObject()) {
            int minItems = fieldSchema.has("minItems") ? fieldSchema.get("minItems").asInt() : 1;

            for (int i = 0; i < minItems; i++) {
                array.add(generateFieldExample(fieldSchema.get("items")));
            }
        }

        return array;
    }
}

